package com.kyzh.core.uis

import android.app.Activity
import android.graphics.Color
import android.graphics.drawable.VectorDrawable
import android.view.Gravity
import android.view.View
import android.view.ViewManager
import android.widget.RelativeLayout
import android.widget.TextView
import com.gushenge.atools.util.AView
import com.gushenge.core.dao.MMKVConsts.statusBarColor
import com.kyzh.core.R
import org.jetbrains.anko.*


class titlebar{

    lateinit var titleT:TextView
    public lateinit var root :RelativeLayout
    lateinit var title :String
    lateinit var close:RelativeLayout
    var vis :Int = 0
    constructor(title:String, visibility: Int){
        this.vis = visibility
        this.title = title
    }
    constructor(title:String){
        this.vis = View.VISIBLE
        this.title = title
    }
    fun init(viewManager: ViewManager,activity: Activity):RelativeLayout{
        root = with(viewManager){
            relativeLayout {
                statusBarColor = Color.WHITE
                backgroundColor = statusBarColor
                fitsSystemWindows = true
                relativeLayout {
                    close = relativeLayout{
                        visibility = vis
                        setOnClickListener { activity.finish() }
                        minimumWidth = dip(45)
                        imageView{
                            imageResource = R.drawable.back
                            if (AView.isLightColor(statusBarColor)){
                                (drawable as VectorDrawable).setTint(Color.BLACK)
                            }else{
                                (drawable as VectorDrawable).setTint(Color.WHITE)
                            }


                        }.lparams(width = dip(20),height = dip(20)){
                            centerInParent()
                        }
                    }.lparams(width = dip(45),height = dip(45))
                    titleT = textView(title){
                        textColor = if (AView.isLightColor(statusBarColor)) Color.BLACK else Color.WHITE
                        textSize = 16f
                        gravity = Gravity.CENTER
                        textAppearance = R.style.Title
                    }.lparams(height = dip(45)){
                        centerInParent()
                    }
                }.lparams(width= matchParent,height = dip(45)){
                    AView.setMargins(this@relativeLayout,0,AView.getStatusBarHeight(activity),0,0)
                }
            }
        }
        return root
    }
}